package com.zhouyiquan.qqclient.service;

import com.zhouyiquan.qqcommon.Message;
import com.zhouyiquan.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * @Author: 周益全 (https://gitee.com/zfovikqdev928)
 * @BugFeedBackEmail: zfovikqdev928@126.com
 * @Date: Create in 2023-01
 * @Description: 该类和服务端提供和消息相关的服务方法
 * @Version:
 */
public class MessageClientService {
    /**
     * 私聊
     * @param chatContent 消息内容
     * @param senderId    发送者
     * @param receiver    接收者
     */
    public void sendMessageToOne(String chatContent, String senderId, String receiver) {
        Message message = new Message();
        message.setMessageType(MessageType.MESSAGE_COMM_MES);
        message.setSender(senderId);
        message.setRecipient(receiver);
        message.setContent(chatContent);
        message.setSendTime(new Date().toString());
        System.out.println(senderId + "对" + receiver + "说：" + chatContent);
        try {
            // 获取到该用户线程对应的 socket outputStream，并发送
            ObjectOutputStream oos =
                    new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 群发
     * @param chatContent 消息内容
     * @param sender      发送者
     */
    public void sendMessageToAll(String chatContent, String sender) {
        Message message = new Message();
        message.setMessageType(MessageType.MESSAGE_To_ALL_MES);
        message.setSender(sender);
        message.setContent(chatContent);
        System.out.println(sender + "对大家说：" + chatContent);
        try {
            // 获取到该用户线程对应的 socket outputStream，并发送
            ObjectOutputStream oos =
                    new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(sender).getSocket().getOutputStream());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
